﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This part contains general features of the project
 */

using System;
using System.Data.SqlServerCe;
using System.IO;

namespace MCServeLib.Objects
{
    public class Database
    {
        private String _Path = "";
        private Boolean _AlreadyExists = false;
        private SqlCeConnection _DatabaseConnection = null;

        public Database(String DatabasePath)
        {
            _Path = DatabasePath;
        }

        public Boolean AlreadyExists
        {
            get
            {
                return _AlreadyExists;
            }
            set
            {
                _AlreadyExists = value;
            }
        }

        //public Boolean Init()
        public DatabaseInformation Init()
        {
            /**
             * Create the database-file
             */
            try
            {
                if (!File.Exists(_Path))
                {
                    StreamWriter DBFile = new StreamWriter(_Path);
                    DBFile.Close();
                    _AlreadyExists = false;
                }
                else
                {
                    _AlreadyExists = true;
                }
            }
            catch (Exception e)
            {
                DatabaseInformation result = new DatabaseInformation(false, e.Message);
                return result;
            }

            /**
             * Open the Database-file
             */
            try
            {
                _DatabaseConnection = new SqlCeConnection();
                _DatabaseConnection.ConnectionString = "Data Source ='" + _Path + "';";
                _DatabaseConnection.Open();
                _DatabaseConnection.Close();

                DatabaseInformation result = new DatabaseInformation(true);
                return result;
            } catch(Exception e)
            {
                DatabaseInformation result = new DatabaseInformation(false, e.Message);
                return result;
            }
        }

        public DatabaseResult Query(String command)
        {
            DatabaseResult result = new DatabaseResult();

            try
            {
                SqlCeCommand cmd = new SqlCeCommand(command, _DatabaseConnection);
                _DatabaseConnection.Open();

                result.DBResults = cmd.ExecuteReader();
                result.Result = true;
                _DatabaseConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                result.Result = false;
                return result;
            }
            finally
            {
                _DatabaseConnection.Close();
            }
        }
    }
}
